﻿using System;
using System.Reactive.Linq;
using System.Windows;
using System.Windows.Input;

namespace RxDemo{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            var xs = Observable.Interval(TimeSpan.FromSeconds(1.0)).ObserveOnDispatcher();

            var mouseDown = from evt in Observable.FromEventPattern<MouseButtonEventHandler, MouseButtonEventArgs>(d => d.Invoke, eh => MouseDown += eh, eh => MouseDown -= eh)
                            select evt.EventArgs.GetPosition(this);

            var mouseUp = from evt in Observable.FromEventPattern<MouseButtonEventHandler, MouseButtonEventArgs>(d => d.Invoke, eh => MouseUp += eh, eh => MouseUp -= eh)
                            select evt.EventArgs.GetPosition(this);

            var mouseMove = from evt in Observable.FromEventPattern<MouseEventHandler, MouseEventArgs>(d => d.Invoke, eh => MouseMove += eh, eh => MouseMove -= eh)
                            select evt.EventArgs.GetPosition(this);

            var positionsQuery = from start in mouseDown
                    from position in mouseMove.StartWith(start).TakeUntil(mouseUp)
                    select position;

            //var deltasQuery = from start in mouseDown
            //                  from delta in mouseMove.StartWith(start).TakeUntil(mouseUp)
            //                  .Let(mm => mm.Zip(mm.Skip(1), (prev, cur) =>
            //                      new { X = cur.X - prev.X, Y = cur.Y - prev.Y }))
            //                  select delta;

            var sb = positionsQuery.Subscribe(x => textBlock.Text = x.ToString());
            //sb.Dispose();
        }
    }
}
